package com.base.app_core.util.retrofit;

import com.base.app_core.util.AppConstants;
import com.base.app_core.util.log.F;
import com.base.app_core.util.security.Base64Utils;
import com.base.app_core.util.security.RSAUtils;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * Created by zhang on 2016/5/31.
 * 自定义响应ResponseBody
 */
public class JsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {

    private final Gson gson;
    private final TypeAdapter<T> adapter;

    JsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody responseBody) throws IOException {

        long l = System.currentTimeMillis();
        Object var3 = null;
        String source = responseBody.string();
        try {
            byte[] decode = Base64Utils.decode(source);
            String destring = new String(RSAUtils.decryptByPrivateKey(decode, AppConstants.privateJavaKey));
            F.http("decode==>" + destring);
            var3 = adapter.fromJson(destring);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            responseBody.close();
        }
        return (T) var3;
    }
}
